# -*- coding:UTF-8 -*-
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/topics/item-pipeline.html


from scrapy.xlib.pydispatch import dispatcher
from scrapy import signals


dirname='./lianjia-bj-ershoufan.csv'
class LianjiaPipeline(object):
    def __init__(self):
        dispatcher.connect(self.spider_opened, signals.spider_opened)
        dispatcher.connect(self.spider_closed, signals.spider_closed)

    def spider_opened(self, spider):
        #fname = path.join(dirname, '-'.join((item['user'], item['id'])))
        fname = dirname
        self.f = file(fname, 'wb')

    def spider_closed(self, spider):
        self.f.close()

    def process_item(self, item, spider):
        #item['content'] = u''.join(line.encode('utf-8') for line in item['content'])
        print "==>",''.join(item['name']).replace(',','').encode('utf-8')
        try:
           self.f.write(''.join(item['price']).replace(',','').encode('utf-8')+(','))
           self.f.write(''.join(item['meters']).replace(',','').encode('utf-8')+(','))
           self.f.write(''.join(item['price_pre']).replace(',','').encode('utf-8')+(','))
           self.f.write(''.join(item['first_pay']).replace(',','').encode('utf-8')+(','))
           self.f.write(''.join(item['month_pay']).replace(',','').encode('utf-8')+(','))
           self.f.write(''.join(item['region']).replace(',','').encode('utf-8')+(','))
           self.f.write(''.join(item['orientation']).replace(',','').encode('utf-8')+(','))
           self.f.write(''.join(item['floor']).replace(',','').encode('utf-8')+(','))
           self.f.write(''.join(item['name']).replace(',','').encode('utf-8')+(","))
           self.f.write(''.join(item['coordx']).replace(',','').encode('utf-8')+(','))
           self.f.write(''.join(item['coordy']).replace(',','').encode('utf-8')+(','))
           self.f.write(''.join(item['link']).replace(',','').encode('utf-8')+(","))
           self.f.write(''.join(item['title']).replace(',','').encode('utf-8')+("\n"))
        except Exception,e:
            print "error:",Exception,":",e
            pass
        return item
